package com.kqb.utils;

import java.util.Map;

/**
 * @author 杉果树皮
 * @date 2025/2/26 18:49
 * @description: ThreadLocal 工具类
 */
public class ThreadLocalUtils {
    // 定义一个 ThreadLocal 变量，用于存储当前用户的信息
    private final static ThreadLocal<Map> currentUser = new ThreadLocal<>();
    private static ThreadLocalUtils threadLocalUtils;

    //私有化构造函数
    private ThreadLocalUtils() {
    }


    //  提供全局访问点，使用 synchronized 保证线程安全
    public static ThreadLocalUtils getThreadLocalUtils() {
        if (threadLocalUtils != null) {
            return threadLocalUtils;
        } else {
            return new ThreadLocalUtils();
        }
    }

    // 设置当前用户
    public void setCurrentUser(Map user) {
        currentUser.set(user);
    }

    // 获取当前用户
    public Map getCurrentUser() {
        return currentUser.get();
    }

    // 清除当前用户信息
    public void clear() {
        currentUser.remove();
    }
}
